package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.ace.json.JSONException;
import org.icefaces.ace.json.JSONObject;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.exception.BaseException;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.QuoteTemplateMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.supplier.QuoteTemplateMasterService;
import com.avecsys.quotemanager.ui.components.QuoteTemplateResource;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.TransactionStatus;

/**
 * @author Sam Sundar K
 */
public class QuoteTemplateMasterListMBean extends BaseMBean implements Serializable
{
	private final Log log = LogFactory.getLog(QuoteTemplateMasterListMBean.class);

	private QuoteTemplateMasterService quoteTemplateMasterService;

	private List<QuoteTemplateWrapper> quoteTemplateWrapperList = new ArrayList<QuoteTemplateWrapper>();

	/**
	 * Action Method to delete the SupplierQuoteTemplateMaster Information
	 * 
	 * @param actionEvent
	 */
	public void actionDeleteQuoteTemplate(ActionEvent actionEvent)
	{
		// Call Common routine
		executePreRoutines();
		String requestJSON = getMessageDialog().getRequestJSON();
		JSONObject requestJSONObject;
		String templateId = null;
		try
		{
			requestJSONObject = new JSONObject(requestJSON);
			templateId = (String) requestJSONObject.get("templateId");
			QuoteTemplateMasterPojo quoteTemplateMasterPojo = quoteTemplateMasterService.getQuoteTemplateMasterById(new Long(
					templateId));
			if (quoteTemplateMasterPojo != null)
			{
				TransactionStatus transactionStatus = quoteTemplateMasterService.deleteQuoteTemplateMaster(quoteTemplateMasterPojo);
				// Reload the Quote Template Inquiry List
				loadAllSupplierQuoteTemplates();
				MessageBoardUtil.publishTransactionStatusMessage(
						transactionStatus,
							EnumActor.QUOTE_TEMPLATE,
							"messageBoardId",
							"",
							new Object[]
							{ quoteTemplateMasterPojo.getTemplateName() });
			}
			else
			{

			}
		}
		catch (BaseException e)
		{
			MessageBoardUtil.publishException(e);
			log.error(e);
		}
		catch (Exception e)
		{
			MessageBoardUtil.publishException(e);
			log.error(e);
		}
		getMessageDialog().hideDialog();
	}

	/**
	 * Action Method to edit the Quote Template Master Information
	 * 
	 * @return Navigation outcome
	 */
	public String actionEditQuoteTemplateMaster()
	{
		// Call Common routine
		executePreRoutines();
		FacesContext faceContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = faceContext.getExternalContext().getRequestParameterMap();
		String templateId = (String) paramValues.get("templateId");
		try
		{
			QuoteTemplateMasterPojo quoteTemplateMasterPojo = quoteTemplateMasterService.getQuoteTemplateMasterById(new Long(
					templateId));
			QuoteTemplateMasterMBean quoteTemplateMasterMBean = (QuoteTemplateMasterMBean) faceContext.getApplication()
					.evaluateExpressionGet(
							faceContext,
								"#{quoteTemplateMasterMBean }",
								QuoteTemplateMasterMBean.class);
			quoteTemplateMasterMBean.setQuoteTemplateMasterPojo(quoteTemplateMasterPojo);
			QuoteTemplateWrapper quoteTemplateWrapper = new QuoteTemplateWrapper(
					quoteTemplateMasterPojo, new QuoteTemplateResource(quoteTemplateMasterPojo));
			quoteTemplateMasterMBean.setQuoteTemplateWrapper(quoteTemplateWrapper);
			quoteTemplateMasterMBean.setModifyExistingQuoteAttachment(false);
			quoteTemplateMasterMBean.setQuoteAttachmentDisabled(true);
		}
		catch (BusinessException e)
		{
			MessageBoardUtil.publishException(e);
			log.error(e);
		}
		catch (Exception e)
		{
			MessageBoardUtil.publishException(e);
			log.error(e);
		}
		return "openEditQuoteTemplateMaster";
	}

	/**
	 * Method to show the confirm dialog request to delete supplier user
	 * 
	 * @param actionEvent
	 */
	public void confirmDeleteAction(ActionEvent actionEvent)
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = facesContext.getExternalContext().getRequestParameterMap();
		String templateId = (String) paramValues.get("templateId");
		String templateName = (String) paramValues.get("templateName");
		JSONObject requestJSONObject = new JSONObject();
		try
		{
			requestJSONObject.put("templateId", templateId);
			requestJSONObject.put("templateName", templateName);
			getMessageDialog().setRequestJSON(requestJSONObject.toString());
			getMessageDialog().setMessage(
					MessageBoardUtil.getValue(
							facesContext,
								ApplicationConstants.RESOURCE_BUNDLE_COMMON,
								ApplicationConstants.ACTION_DELETE_CONFIRMATION,
								new Object[]
								{ templateName }));
		}
		catch (JSONException e)
		{
			log.error(e);
		}
		getMessageDialog().showDialog();
	}

	/**
	 * @return the quoteTemplateMasterService
	 */
	public QuoteTemplateMasterService getQuoteTemplateMasterService()
	{
		return quoteTemplateMasterService;
	}

	/**
	 * @return the quoteTemplateWrapperList
	 */
	public List<QuoteTemplateWrapper> getQuoteTemplateWrapperList()
	{
		return quoteTemplateWrapperList;
	}

	/**
	 * Method to load all the users for a supplier
	 * 
	 * @param supplierMasterPojo
	 * @throws BusinessException
	 */
	public void loadAllSupplierQuoteTemplates()
	{
		UserMasterPojo userMasterPojo = getSignedInUser();
		getQuoteTemplateWrapperList().clear();
		if (userMasterPojo != null)
		{
			for (SupplierUsersPojo supplierUsersPojo : userMasterPojo.getSupplierUsersPojo())
			{
				SupplierMasterPojo supplierMasterPojo = supplierUsersPojo.getSupplierMasterPojo();
				List<QuoteTemplateMasterPojo> quoteTemplateMasterPojoList;
				try
				{
					quoteTemplateMasterPojoList = quoteTemplateMasterService.getAllSupplierQuoteTemplates(supplierMasterPojo);
					for (QuoteTemplateMasterPojo quoteTemplateMasterPojo : quoteTemplateMasterPojoList)
					{
						QuoteTemplateWrapper quoteTemplateWrapper = new QuoteTemplateWrapper(
								quoteTemplateMasterPojo, new QuoteTemplateResource(
										quoteTemplateMasterPojo));
						getQuoteTemplateWrapperList().add(quoteTemplateWrapper);
					}
				}
				catch (BusinessException e)
				{
					MessageBoardUtil.publishException(e);
				}
			}
		}
	}

	/**
	 * @param quoteTemplateMasterService the quoteTemplateMasterService to set
	 */
	public void setQuoteTemplateMasterService(QuoteTemplateMasterService quoteTemplateMasterService)
	{
		this.quoteTemplateMasterService = quoteTemplateMasterService;
	}

	/**
	 * @param quoteTemplateWrapperList the quoteTemplateWrapperList to set
	 */
	public void setQuoteTemplateWrapperList(List<QuoteTemplateWrapper> quoteTemplateWrapperList)
	{
		this.quoteTemplateWrapperList = quoteTemplateWrapperList;
	}
}
